//
//  MTTopUpInputNumberView.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/23.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTTopUpInputNumberView.h"
#import "MTWebViewController.h"
#import "UIView+ViewController.h"
#import "MTPayOrderModel.h"
#import "MTWebPayViewController.h"

@interface MTTopUpInputNumberView ()

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *menonyLable;
@property (weak, nonatomic) IBOutlet UILabel *paytipLable;

@property(nonatomic,strong) KLCPopup *pop;

@property(nonatomic,assign) double rate;



@end

@implementation MTTopUpInputNumberView

+(instancetype)topUpInputNumberView{
    MTTopUpInputNumberView *view = [[NSBundle mainBundle] loadNibNamed:@"MTTopUpInputNumberView" owner:nil options:nil].firstObject;
    view.width = WIDTH;
    view.height = RelativeValue(HEIGHT - 150);
    return view;
}

-(void)show
{
    KLCPopup *newpop = [KLCPopup popupWithContentView:self];
    newpop.showType = KLCPopupShowTypeSlideInFromBottom;
    newpop.dismissType = KLCPopupDismissTypeSlideOutToBottom;
    newpop.maskType = KLCPopupMaskTypeDimmed;
    self.pop = newpop;
    [newpop showAtCenter:CGPointMake(WIDTH/2.0, HEIGHT - self.frame.size.height/2) inView:[[UIApplication sharedApplication].delegate window]];
    [self.textField becomeFirstResponder];
    [[MTWalletApiManager manager] getRechargeRatecomplation:^(double rate, MTError *error) {
        if(rate > 0)
        {
            self.rate = rate;
            self.paytipLable.hidden = NO;
            self.menonyLable.hidden = NO;
        }else
        {
            self.paytipLable.hidden = YES;
            self.menonyLable.hidden = YES;
        }
    }];
    
    @weakify(self);
    [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        NSInteger volue = [x integerValue];
        self.menonyLable.text = [NSString stringWithFormat:@"%.2lf",volue * _rate];
    }];

}

-(void)dismis{
    [self.textField resignFirstResponder];
    [self.pop dismiss:YES];
}



- (IBAction)closeClick:(UIButton *)sender {
    [self dismis];
}
- (IBAction)nextClick:(UIButton *)sender {
//    [self show];
    
    if(self.textField.text.length > 0 && [self.textField.text integerValue] > 0)
    {
        
        [[MTWalletApiManager manager] createRechargeWithSymbol:_model.symbol value:self.textField.text complation:^(MTPayOrderModel *payOrder, MTError *error) {
            self.textField.text = nil;
           if(error == nil)
           {
               [self dismis];
               MTWebPayViewController *webVc = [[MTWebPayViewController alloc] init];
               webVc.payModel = payOrder;
               [self.presentView.currentViewController.navigationController pushViewController:webVc animated:YES];
           }else
           {
               [MBProgressHUD showError:error.errorDescription];
           }
        }];
    }
    
    
    
}

@end
